from collections import Iterable


class G:
    class S:
        @staticmethod
        def _get(d, k):
            if isinstance(d, dict):
                return d.get(k)
            elif isinstance(d, list) and k < len(d):
                return d[k]
            return None

        @staticmethod
        def get(d: dict, k, default=None) -> any:
            if not isinstance(d, (dict, list)):
                return default
            if isinstance(k, (list, tuple)):
                r = d
                for key in k:
                    r = G.S._get(r, key)
                    if r is None:
                        return default
                return r
            else:
                r = G.S._get(d, k)
                if r is None:
                    return default
                return r
            return default

        @staticmethod
        def _dict_get(d: dict, k):
            if not isinstance(d, dict):
                return None
            return d.get(k)

        @staticmethod
        def safe_object_dot(o: object, k: str, default=None):
            if not isinstance(k, str):
                return default
            return getattr(o, k, default)

        @staticmethod
        def safe_in(it: Iterable, k) -> bool:
            if not isinstance(it, Iterable):
                return False
            return k in it

        @staticmethod
        def safe_dict_get(d: dict, k, default=None) -> any:
            """
            替换dict.get
            """
            if not isinstance(d, dict):
                return default
            if isinstance(k, (list, tuple)):
                r = d
                for key in k:
                    r = G.S._dict_get(r, key)
                    if r is None:
                        return default
                return r
            else:
                r = G.S._dict_get(d, k)
                if r is None:
                    return default
                return r
            return default

        @staticmethod
        def safe_dict_del(d: dict, k):
            """
            替换del d[k] 只支持删除str类型的key
            params:
                d: dict
                k: list(str) or str
            """
            if isinstance(d, dict):
                if isinstance(k, list):
                    if len(k) == 1:
                        G.S.safe_dict_del(d, k[0])
                    elif len(k) == 0:
                        return
                    else:
                        d = G.S.safe_dict_get(d, k[0])
                        G.S.safe_dict_del(d, k[1:])
                elif isinstance(k, str):
                    try:
                        del d[k]
                    except Exception:
                        pass

        @staticmethod
        def safe_list_get(l: list, i: int, default=None, log_exception=False):
            try:
                return l[i]
            except Exception:
                return default

        @staticmethod
        def int_or_0(v):
            try:
                return int(v)
            except Exception:
                return 0

        @staticmethod
        def safe_int(v, default=0):
            try:
                return int(v)
            except Exception:
                return default

        @staticmethod
        def safe_float(v, default=0):
            try:
                return float(v)
            except Exception:
                return default

    class C:  # Constant 公共常量
        CURSOR_START = -1
        CURSOR_SECEND_START = -2
        CURSOR_END = '0'
        INT64_MAX = (1 << 64) - 1
